package com.starview.star.operators.binary.runtime;

import com.starview.platform.data.EvaluationException;
import com.starview.platform.data.IFunction;
import com.starview.platform.data.IValue;
import com.starview.platform.data.type.IType;
import com.starview.platform.data.type.StandardTypes;
import com.starview.platform.data.type.TypeUtils;
import com.starview.platform.data.value.BinaryWrap;
import com.starview.platform.data.value.BinaryWrap.BinaryWrapper;
import com.starview.platform.data.value.BinaryWrap.NonBinaryWrapper;
import com.starview.platform.data.value.BoolWrap;
import com.starview.platform.data.value.Factory;
import com.starview.star.operators.CafeEnter;

public abstract class BinaryEquals implements IFunction
{

  @CafeEnter
  public static BoolWrap enter(BinaryWrap l, BinaryWrap r) throws EvaluationException
  {
    if (l instanceof BinaryWrapper && r instanceof BinaryWrapper) {
      Object lContent = ((BinaryWrapper) l).getValue();
      Object rContent = ((BinaryWrapper) r).getValue();

      if (lContent != null) {
        if (rContent != null)
          return Factory.newBool(lContent.equals(rContent));
        else
          return BoolWrap.falseEnum;
      } else
        return Factory.newBool(rContent == null);
    } else
      return Factory.newBool(l instanceof NonBinaryWrapper && r instanceof NonBinaryWrapper);
  }

  @Override
  public IType getType()
  {
    return TypeUtils.functionType(StandardTypes.binaryType, StandardTypes.binaryType, StandardTypes.booleanType);
  }

  @Override
  public IValue enter(IValue... args) throws EvaluationException
  {
    return enter((BinaryWrap) args[0], (BinaryWrap) args[1]);
  }
}
